package validator;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import db.ImbiConnection;

public class UserAccountValidator {
	Integer id = null;
	Integer subject = null;
	Integer status = null;
	Date validFrom = null;
	Date validTo  = null;
	Boolean passwordNeverExpires = null;
	String user = null;
	String pass = null;
	
	public static final SimpleDateFormat dateFormat = new SimpleDateFormat("MMMMM dd, yyyy", Locale.ENGLISH);
		
	public UserAccountValidator(Object id, Object subject, Object status, Object validFrom, Object validTo, Object passwordNeverExpires, Object password, Object user){
		try{			
			this.id = Integer.parseInt((String)id);			
			if(this.id <= 0) this.id = null;
		} catch(Exception e){ }
		try{
			this.subject = Integer.parseInt((String)subject);			
			if(this.subject < 0) this.subject = null;
			this.subject = validateSubject() ? this.subject : null;
			
		} catch(Exception e){ }
		try{
			this.status = Integer.parseInt((String)status);
			if(this.status < 0) this.status = null;
		} catch(Exception e){ }
		try{
			this.validFrom = dateFormat.parse((String)validFrom);
		} catch(Exception e){ }
		try{
			this.validTo = dateFormat.parse((String)validTo);
		} catch(Exception e){ }
		try{		
			String str = (String)passwordNeverExpires;
			this.passwordNeverExpires = str.equals("1");
		} catch(Exception e){ }
		try{		
			this.user = (String)user;			
		} catch(Exception e){ }
		try{		
			this.pass = (String)password;			
		} catch(Exception e){ }		
	}
	
	private boolean validateSubject() {		
		return ImbiConnection.getEmployeeDAO().find(subject) != null;
	}
	
	public boolean isValidNewUser(){
		return isValidUserUpdate() && user != null && user.length() >= 3 && pass != null && pass.length() >= 5;
	}
	
	public boolean isValidUserUpdate(){
		return (id != null || (user != null && pass != null)) && 
				subject != null && 
				status != null && 			
				validTo != null &&
				validFrom != null &&
				passwordNeverExpires != null;
	}

	public Integer getId() {
		return id;
	}

	public Integer getSubject() {
		return subject;
	}

	public String getUser() {
		return user;
	}

	public String getPass() {
		return pass;
	}

	public Integer getStatus() {
		return status;
	}

	public Date getValidFrom() {
		return validFrom;
	}

	public Date getValidTo() {
		return validTo;
	}

	public Boolean getPasswordNeverExpires() {
		return passwordNeverExpires;
	}

	public static SimpleDateFormat getDateformat() {
		return dateFormat;
	}
	
}
